package com.zhiyou100.weather.job;

import com.zhiyou100.weather.mapper.CityMapper;
import com.zhiyou100.weather.pojo.City;
import com.zhiyou100.weather.service.IWeatherDataService;
import com.zhiyou100.weather.utils.RedisUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

public class WeatherJob implements Job {
    private static Logger logger = LoggerFactory.getLogger(WeatherJob.class);
    @Resource
    private CityMapper cityMapper;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private IWeatherDataService iWeatherDataService;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String URL = "http://wthrcdn.etouch.cn/weather_mini?citykey=";
        List<City> cities = cityMapper.queryAll();
        long start = System.currentTimeMillis();
        for (City city : cities) {
            //查询拉取数据
            ResponseEntity<String> entity = restTemplate.getForEntity(URL + city.getId(), String.class);
            String body = entity.getStatusCodeValue() == 200 ? entity.getBody() : null;
            //封装到Redis中
            redisTemplate.opsForValue().set(URL + city.getId(), body);
        }
        long end = System.currentTimeMillis();
        System.out.println(123);
        logger.info("抓取数据总耗时==》" + (end - start) / 1000 / 60);


       /* String URL = "http://wthrcdn.etouch.cn/weather_mini?citykey=";
        List<City> cities = cityMapper.queryAll();
        long start = System.currentTimeMillis();
        for (City city : cities) {
            //查询拉取数据
            WeatherResponse body = iWeatherDataService.getDataByCityId(URL + city.getId());
            HashMap map = new HashMap<>();
//            map.put("data", body.getData());
            map.put("status", body.getStatus());
            map.put("desc", body.getDesc());
            //封装到Redis中
            redisUtils.hmset(URL+city.getId(), map);
            System.out.println("输出成功");
        }*/
    }
}
